package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryApplyOutWarehouseAdapter
import com.c.ctgproject.adapter.guang.BatteryInWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.base.MyBaseApplication
import com.c.ctgproject.databinding.ActivityReturnApplyBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import okhttp3.MediaType
import okhttp3.RequestBody

class ReturnApplyActivity : BaseActivityV2() ,BatteryApplyOutWarehouseAdapter.CallBack{
    private val mBinding by lazy { ActivityReturnApplyBinding.inflate(layoutInflater) }
    private val mViewModel by lazy { setViewModel<PowerBatteryInWarehouseViewModel>(this) }
    private var page = 1
    private var search: String = ""
    //是否审批
    private val searchType by lazy { intent.getIntExtra("searchType",0) }
    private var mList = ArrayList<BatteryInWarehouseEntity.DataBean.ListBean>()
    private val mAdapter by lazy { BatteryApplyOutWarehouseAdapter(this, mList,this,false) }
    val selectList=ArrayList<Int>()
    private var auditPersonName=""
    override fun getLayout(): View = mBinding.root
    override fun event() {
        if(searchType==1){
            //mBinding.mWaitApplyTab.visibility=View.GONE
            mBinding.mCheckerLL.visibility=View.GONE
            mBinding.mTabLayout.removeTabAt(1)
            mBinding.mConfirmBTN.text="同意"
            mBinding.mRefusedBTN.visibility=View.VISIBLE
            mBinding.mTitleTV.setTitle("退库审核")
        }else{
            mBinding.mCheckerLL.visibility=View.VISIBLE
            mBinding.mTabLayout.removeTabAt(0)
            //mBinding.mWaitAuditTab.visibility=View.GONE
            mBinding.mRefusedBTN.visibility=View.GONE
        }
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)
        getData()
    }
    var searchMine = false
    override fun click() {
        //选择审批人
        mBinding.mCheckerLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.CHECK_USER.menuCode, true, arrayListOf(auditPersonName), resultCode = 104, partFrom = 4, canInput = false)
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }

        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "待审核"->{
                    mBinding.mCheckerLL.visibility=View.GONE
                    searchMine = false
                    mBinding.mBottomRL.visibility=View.VISIBLE
                    mAdapter.setIsMine(searchMine)
                    mViewModel.changeSearchMine(searchMine)
                    page = 1
                    getData()
                }
                "待申请" -> {
                    mBinding.mCheckerLL.visibility=View.VISIBLE
                    searchMine = false
                    mBinding.mBottomRL.visibility=View.VISIBLE
                    mAdapter.setIsMine(searchMine)
                    mViewModel.changeSearchMine(searchMine)
                    page = 1
                    getData()
                }
                "我的" -> {
                    mBinding.mCheckerLL.visibility=View.GONE
                    searchMine = true
                    mAdapter.setIsMine(searchMine)
                    mBinding.mBottomRL.visibility=View.GONE
                    mViewModel.changeSearchMine(searchMine)
                    page = 1
                    getData()
                }
            }
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
        mBinding.mRefusedBTN.setOnClickListener {
            if(selectList.size==0){
                showToast("请先选择电池")
                return@setOnClickListener
            }
            val requestBody = RequestBody.create(MediaType.parse("application/json"), getGson().toJson(selectList))
            ApiMethods.apiSubscribe(ApiMethods.retrofit.pbApply(requestBody,2), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("驳回结果:$it")
                val result= getGson().fromString<ResultEntity<String>>(it)
                if(result.status==1){
                    showToast("驳回成功")
                    selectList.clear()
                    page=1
                    getData()
                }else{
                    showToast(result.msg)
                }
            })
        }
        mBinding.mConfirmBTN.setOnClickListener {
            if(selectList.size==0){
                showToast("请先选择电池")
                return@setOnClickListener
            }
            val requestBody = RequestBody.create(MediaType.parse("application/json"), getGson().toJson(selectList))
            if(searchType==1){
                ApiMethods.apiSubscribe(ApiMethods.retrofit.pbApply(requestBody,1), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                    MyLog.d("审核结果:$it")
                    val result= getGson().fromString<ResultEntity<String>>(it)
                    if(result.status==1){
                        showToast("审核成功")
                        selectList.clear()
                        page=1
                        getData()
                    }else{
                        showToast(result.msg)
                    }
                })
            }else{
                if(auditPersonName.isEmpty()){
                    showToast("请先选择审批人员")
                    return@setOnClickListener
                }
                ApiMethods.apiSubscribe(ApiMethods.retrofit.pbApply(requestBody,0,auditPersonName), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                    MyLog.d("申请结果:$it")
                    val result= getGson().fromString<ResultEntity<String>>(it)
                    if(result.status==1){
                        showToast("申请成功")
                        selectList.clear()
                        page=1
                        getData()
                    }else{
                        showToast(result.msg)
                    }
                })
            }
        }
    }
    fun getData(){
        if (searchMine) {
            //查询类型 2-待过磅 3-待入库 1.默认我的录入
            val searchWay=if(searchType==1)ApiMethods.retrofit.powerBatteryList(search, page, returnUserName = MyBaseApplication.userName)else ApiMethods.retrofit.powerBatteryList(search, page,applyReturnName=MyBaseApplication.userName, dismantle = 0)
            ApiMethods.apiSubscribe(searchWay, MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我申请/审核的电池:$it")
                if (page == 1) mList.clear()
                val data = getGson().fromJson(it, BatteryInWarehouseEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            })
        }else{
            val searchWay=if(searchType==1)ApiMethods.retrofit.powerBatteryList(search, page, returnStatus=0,returnUserName=MyBaseApplication.userName, batteryStatus = 1, dismantle = 0)else ApiMethods.retrofit.powerBatteryList(search, page, returnStatus = 3, batteryStatus = 1, dismantle = 0)

            //查询类型 2-待过磅 3-待入库 1.默认我的录入
            ApiMethods.apiSubscribe(searchWay, MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("申请退库、待审核电池:$it")
                if (page == 1) mList.clear()

                val data = getGson().fromJson(it, BatteryInWarehouseEntity::class.java).data
                data?.list?.let { list ->
                    list.forEach {

                        if(selectList.contains(it.pbId)){
                            it.checked=true
                        }
                        mList.add(it)
                    }

                    mAdapter.notifyDataSetChanged()
                }
            })
        }
    }

    override fun itemCheckSelect(position: Int, data: BatteryInWarehouseEntity.DataBean.ListBean) {
        if(data.checked){
            if(!selectList.contains(data.pbId)){
                selectList.add(data.pbId)
            }
        }else{
            if(selectList.contains(data.pbId)){
                selectList.remove(data.pbId)
            }
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let { result ->
            when (requestCode) {
                104 -> {
                    //审批人员回调
                    val name = result.getStringExtra(MyConstants.NAME).handlerNull()
                    val abbreviation = result.getStringExtra(MyConstants.ABBREVIATION).handlerNull()
                    auditPersonName = name
                    //auditPersonId = abbreviation.toInt()
                    mBinding.mCheckerTV.text = name
                }
            }
        }
    }
}